﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain.Management;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using MediatR;
using MedicalPCTerminal.Write.API.Appcation.Commands.Management;

namespace MedicalPCTerminal.Write.API.Appcation.CommandHandlers.Management
{
    /// <summary>
    /// 处理接口方法
    /// </summary>
    public class AddBasicCommandHandler : IRequestHandler<AddBasicCommand, APIResult<object>>
    {
        private readonly SqlSugarContext sqlSugar;
        private readonly IMapper mapper;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="sqlSugar"></param>
        /// <param name="mapper"></param>
        public AddBasicCommandHandler(SqlSugarContext sqlSugar, IMapper mapper)
        {
            this.sqlSugar = sqlSugar;
            this.mapper = mapper;
        }
        public Task<APIResult<object>> Handle(AddBasicCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result=new APIResult<object>();
            try
            {
                var entity = mapper.Map<Basic>(request);
                entity.Status = "未随访";
                var res = sqlSugar.db.Insertable(entity).ExecuteCommand();
                if (res > 0)
                {
                    result.Code = APIEnum.添加成功;
                    result.Msg = APIEnum.添加成功.ToString();
                    result.data = entity;
                }
                else
                {
                    result.Code = APIEnum.添加失败;
                    result.Msg = APIEnum.添加失败.ToString();
                }
                return Task.FromResult(result);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
